package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.*;
import com.woniuxy.common.mapper.ShopOrderItemMapper;
import com.woniuxy.common.mapper.ShopOrderMapper;
import com.woniuxy.joylifeshop.dto.ObjectDto;
import com.woniuxy.joylifeshop.exception.OrderException;
import com.woniuxy.joylifeshop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    ShopOrderMapper shopOrderMapper;

    @Autowired
    ShopOrderItemMapper shopOrderItemMapper;


    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public List<ObjectDto> readAllNeedOrder(String userId,String status) {


        try {
            List<ObjectDto> objectDtos = new ArrayList<>();
            //找到所有未支付的订单
            ShopOrderExample shopOrderExample = new ShopOrderExample();
            ShopOrderExample.Criteria criteria = shopOrderExample.createCriteria();
            criteria.andIdEqualTo(userId);
            criteria.andStatusEqualTo(status);
            List<ShopOrder> shopOrders = shopOrderMapper.selectByExample(shopOrderExample);
            //找到这些订单的条目
            for (ShopOrder shopOrder:shopOrders){
                ShopOrderItemExample shopOrderItemExample = new ShopOrderItemExample();
                ShopOrderItemExample.Criteria criteria2 = shopOrderItemExample.createCriteria();
                criteria2.andOrderIdEqualTo(shopOrder.getOrderId());
                List<ShopOrderItem> shopOrderItems = shopOrderItemMapper.selectByExample(shopOrderItemExample);

                ObjectDto objectDto = new ObjectDto();
                objectDto.setData(shopOrder);
                objectDto.setData2(shopOrderItems);

                objectDtos.add(objectDto);
            }

            return objectDtos;
        } catch (Exception e) {
            e.printStackTrace();
            throw new OrderException();
        }
    }
    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public void modifyRefundStatus(String orderId) {

        try {
            //找到订单对象
            ShopOrder shopOrder = shopOrderMapper.selectByPrimaryKey(orderId);
            //修改
            shopOrder.setStatus("退货处理中");
            //存入
            shopOrderMapper.updateByPrimaryKey(shopOrder);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OrderException();
        }


    }
    /**
    * @Author SooYoo
    * @Description 待发货改为待收货
    * @Param
    * @Return
    **/
    @Override
    public void makepost(String orderId) {

        ShopOrder shopOrder = shopOrderMapper.selectByPrimaryKey(orderId);
        shopOrder.setStatus("待收货");
        shopOrderMapper.updateByPrimaryKey(shopOrder);

    }

    @Override
    public void makereceiver(String orderId) {

        ShopOrder shopOrder = shopOrderMapper.selectByPrimaryKey(orderId);
        shopOrder.setStatus("已收货");
        shopOrderMapper.updateByPrimaryKey(shopOrder);
    }


    /**
    * @Author SooYoo
    * @Description 将未支付的订单修改为支付的订单
    * @Param
    * @Return
    **/
    @Override
    public void modifyUnPayOrder(ShopAddress shopAddress, String orderId) {

        try {
            ShopOrder shopOrder = shopOrderMapper.selectByPrimaryKey(orderId);
            shopOrder.setStatus("待发货");
            shopOrder.setAddress(shopAddress.getCountry()+shopAddress.getProvince()+shopAddress.getCity()+shopAddress.getDetail());
            shopOrderMapper.updateByPrimaryKey(shopOrder);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OrderException();
        }


    }

    /**
    * @Author SooYoo
    * @Description 初始化一个order
    * @Param
    * @Return
    **/
    @Override
    public ObjectDto readOneOrderInfo(String orderId) {

        try {
            ObjectDto objectDto = new ObjectDto();
            //订单对象
            ShopOrder shopOrder = shopOrderMapper.selectByPrimaryKey(orderId);
            //订单条目对象
            ShopOrderItemExample shopOrderItemExample = new ShopOrderItemExample();
            ShopOrderItemExample.Criteria criteria = shopOrderItemExample.createCriteria();
            criteria.andOrderIdEqualTo(orderId);
            List<ShopOrderItem> shopOrderItems = shopOrderItemMapper.selectByExample(shopOrderItemExample);

            objectDto.setData(shopOrder);
            objectDto.setData2(shopOrderItems);

            return objectDto;

        } catch (Exception e) {
            e.printStackTrace();
            throw new OrderException();
        }
    }


    /**
    * @Author SooYoo
    * @Description 将订单存入数据库
    * @Param
    * @Return
    **/
    @Override
    public void createOrder(ShopOrder shopOrder, List<ShopOrderItem> shopOrderItems) {

        try {
            shopOrderMapper.insert(shopOrder);
            for (ShopOrderItem shopOrderItem:shopOrderItems){
                shopOrderItemMapper.insert(shopOrderItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new OrderException();
        }


    }


}
